/*
 * @Author: Jone XIn
 * @Date: 2022-01-26 21:13:34
 * @LastEditTime: 2022-01-26 23:33:14
 * @Description: 服务节点事件全集
 */
const initServerEvent = (graph) => {
    // 初始化Mid存进session 中
    initMid();

    // cancel
    $("#server_cancel").click(() => {
        // 清空选项框
        clearAddExitServerDialog();
    });

    // confirm
    $("#server_confirm").click(async () => {
        const project_path = $("#projectPath").val().replaceAll(/\\/g, '\\');
        const ignore_list = $("#ignore").val();
        const res = await axios.post('http://127.0.0.1:4399/device/addRoom', {
            data: {
                project_path,
                ignore_list,
                m_id: window.localStorage.getItem('mId')
            }
        })
        // if (res.data.code == 200) {
        //     clearAddExitServerDialog();
        //     // 增加项目子节点
        //     graph.addProject(res.data.data.project_name, window.localStorage.getItem('mId'), res.data.data.sync_id);
        //     return;
        // }
        alert(res.data.message);
    });
}

// 初始化本机环境
async function initMid() {
    const res = await axios.get('http://127.0.0.1:4399/device/getDeviceInfo');
    if (res.data.code == 200) {
        window.localStorage.setItem('mId', res.data.data.mid);
        window.localStorage.setItem('ip', res.data.data.ip);
        return;
    }
    alert('未获取到本机信息，请联系管理员')
    console.log(22222);
}

function clearAddExitServerDialog() {
    $("#projectPath").val('');
    $("#ignore").val('');
    $("#add_project").hide();
}


export default initServerEvent;
